<?php

namespace backend\controllers;

use backend\models\MallBusiness;
use Yii;
use backend\models\EduUser;
use backend\models\EduUserSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * EduUserController implements the CRUD actions for EduUser model.
 */
class EduUserController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all EduUser models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new EduUserSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single EduUser model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new EduUser model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new EduUser();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing EduUser model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
            if ($model->mall_is == 1) {
                $model->mall_enter_time = time();
                $model->mall_create_time = $model->mall_create_time ? $model->mall_create_time : time();

                //查询代理商表 有就创建 没有就修改
                $MallBusinessModel = MallBusiness::find()->where(['uid' => $id])->one();
                if (null == $MallBusinessModel) {
                    //需要创建
                    $shopModel = new  MallBusiness();
                    $shopModel->uid = $id;
                    $shopModel->avatar = $model->avatar_url;
                    $shopModel->name = $model->nickname . "-旗舰店";
                    $shopModel->update_time = time();
                    $shopModel->create_time = time();
                    $shopModel->status = 1;
                    $shopModel->ad = Yii::$app->params['storeAd'];
                    $shopModel->save();
                } else {
                    //将状态改为1
                    $MallBusinessModel->avatar = $model->avatar_url;
                    $MallBusinessModel->ad = Yii::$app->params['storeAd'];
                    $MallBusinessModel->name = $model->nickname . "-旗舰店";
                    $MallBusinessModel->update_time = time();
                    $MallBusinessModel->status = 1;
                    $MallBusinessModel->save();
                }
            } else {
                //查询代理商表 有就创建 没有就修改
                $MallBusinessModel = MallBusiness::find()->where(['uid' => $id])->one();
                if (null != $MallBusinessModel) {
                    //需要status = -1
                    $MallBusinessModel->status = -1;
                    $MallBusinessModel->save();

                }
            }

            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing EduUser model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the EduUser model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return EduUser the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = EduUser::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
